//
// Created by yk120 on 2024/3/1.
//

#include <nanofaiss/utils/random.h>


namespace faiss {


void rand_perm(int *perm, size_t n, int64_t seed) {
    for (size_t i = 0; i < n; i++)
        perm[i] = i;

    RandomGenerator rng(seed);

    // swap the elements
    for (size_t i = 0; i + 1 < n; i++) {
        int i2 = i + rng.rand_int(n - i);
        std::swap(perm[i], perm[i2]);
    }
}


RandomGenerator::RandomGenerator(int64_t seed) : mt(static_cast<unsigned>(seed)){}

int RandomGenerator::rand_int() {
    return mt() & 0x7fffffff;
}

int RandomGenerator::rand_int(int max) {
    return mt() % max;
}

}
